const app = getApp();
var api = require("../../api/api.js");
var utils = require("../../utils/utils.js");
var storageManage = require("../../manage/storageManage.js");

var interval = null;//倒计时函数
var disabled;
var isbind;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    mobile: '',
    seqNo: "",
    vertify_des: "获取验证码",
    currentTime: 61
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(storageManage.getServiceUser().user_id);
    isbind = options.isbind;

  },


  getCode: utils.throttle( function () {
    var that = this;
    var currentTime = that.data.currentTime
    interval = setInterval(function () {
      currentTime--;
      that.setData({
        vertify_des: currentTime + '秒',


      });
      disabled = true;
      if (currentTime <= 0) {
        clearInterval(interval)
        that.setData({
          vertify_des: '重新发送',
          currentTime: 61,

        });
        disabled = false;

      }
    }, 1000)
  }),



  phoneInput: function (e) {
    this.setData({
      mobile: e.detail.value
    })
  },

  vertifyInput: function (e) {

    this.setData({
      vertifyCode: e.detail.value
    })
  },

  getVertify: utils.throttle(function () {
    if (!utils.isTel(this.data.mobile)) {
      wx.showToast({
        title: '请输入正确的手机号',
        icon: 'none'
      })
      return;
    }

    if (disabled)
      return;
    this.getCode();
    api.default.api('send_serial_code', { "mobile_number": this.data.mobile }, this.feedbackSuccessCallback, this.feedbackErrorCallback)
  }),

  feedbackSuccessCallback: function (data) {
    this.setData({
      seqNo: data.data.seq_no
    });
    console.log("seq_no==" + this.data.seqNo);

  },

  bind: utils.throttle(function () {
   
    if (!this.check())
      return;
    api.default.api('check_code', {
      "mobile_number": this.data.mobile,
      "seq_no": this.data.seqNo,
      "verify_code": this.data.vertifyCode,
      "user_id": storageManage.getServiceUser().user_id,
      "avatar": storageManage.getInfo().avatarUrl,
      "nickname": storageManage.getInfo().nickName
    }, this.setSuccessCallback, this.setFailCallback)
  }),



  setSuccessCallback: function (data) {
    if (data.e==0){
      if (data.data.user_type == 0) {
        wx.navigateTo({
          url: '../perfect/perfect',
        })
      } else {
        var serviceUser = storageManage.getServiceUser();
        serviceUser.login=true;
        storageManage.setServiceUser(serviceUser);
        wx.switchTab({
          url: '../index/index'
        })
      }
    }else{
      wx.showToast({
        title: data.ed,
        icon: 'none'
      })
    }
    
    
  },

  check: function () {
    if (!utils.isTel(this.data.mobile)) {
      wx.showToast({
        title: '请输入正确的手机号',
        icon: 'none'
      })
      return false;
    }
    if (utils.isEmpty(this.data.vertifyCode)) {
      wx.showToast({
        title: '请输入验证码',
        icon: 'none'
      })
      return false;
    }
    if (utils.isEmpty(this.data.seqNo)) {
      wx.showToast({
        title: '验证码过期',
        icon: 'none'
      })
      return false;
    }
    return true;
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    disabled = false;
    if (interval != null)
      clearInterval(interval);
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    if (interval != null)
      clearInterval(interval);

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {


  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})